/**
 * 宠物渲染器测试示例
 * 这个文件展示如何测试渲染器的各个功能
 */

// 注意：这只是示例代码，实际测试需要在uni-app环境中运行

/**
 * 测试1：基础渲染
 */
function testBasicRendering() {
  console.log('=== Test 1: Basic Rendering ===')
  
  // 创建Canvas上下文
  const ctx = uni.createCanvasContext('testCanvas')
  
  // 创建渲染器
  const renderer = new PetRenderer(ctx, {
    width: 100,
    height: 100,
    debugMode: true
  })
  
  // 渲染一帧
  renderer.render(performance.now(), 16.67)
  
  console.log('✓ Basic rendering test passed')
}

/**
 * 测试2：动画切换
 */
function testAnimationSwitch() {
  console.log('=== Test 2: Animation Switch ===')
  
  const ctx = uni.createCanvasContext('testCanvas')
  const renderer = new PetRenderer(ctx)
  
  // 测试所有动画类型
  const animations = ['idle', 'happy', 'excited', 'sad', 'dragging']
  
  animations.forEach(anim => {
    renderer.updateAnimation(anim, performance.now())
    console.log(`✓ Switched to ${anim} animation`)
  })
  
  console.log('✓ Animation switch test passed')
}

/**
 * 测试3：性能监控
 */
function testPerformanceMonitoring() {
  console.log('=== Test 3: Performance Monitoring ===')
  
  const monitor = new PerformanceMonitor()
  
  // 模拟60帧
  for (let i = 0; i < 60; i++) {
    monitor.update(performance.now() + i * 16.67)
    monitor.recordRenderTime(10 + Math.random() * 5)
  }
  
  const metrics = monitor.getMetrics()
  console.log('FPS:', metrics.fps)
  console.log('Avg render time:', metrics.avgRenderTime.toFixed(2), 'ms')
  
  console.log('✓ Performance monitoring test passed')
}

/**
 * 测试4：渲染循环
 */
function testRenderLoop() {
  console.log('=== Test 4: Render Loop ===')
  
  const renderLoop = new RenderLoop({
    targetFPS: 60,
    autoAdjustFPS: true
  })
  
  // 创建模拟渲染器
  const mockRenderer = {
    render: (timestamp, deltaTime) => {
      console.log('Rendering frame at', timestamp)
    }
  }
  
  // 添加渲染器
  renderLoop.addRenderer(mockRenderer)
  
  // 启动渲染循环
  renderLoop.start()
  
  // 2秒后停止
  setTimeout(() => {
    renderLoop.stop()
    const metrics = renderLoop.getMetrics()
    console.log('Final FPS:', metrics.fps)
    console.log('✓ Render loop test passed')
  }, 2000)
}

/**
 * 测试5：性能优化
 */
function testOptimization() {
  console.log('=== Test 5: Optimization ===')
  
  const optimizer = new OptimizationManager()
  
  console.log('Device performance level:', optimizer.getPerformanceLevel())
  console.log('Recommended FPS:', optimizer.getRecommendedFPS())
  
  // 测试低性能策略
  optimizer.applyStrategy('lowPerformance')
  console.log('Should simplify gradients:', optimizer.shouldSimplifyGradients())
  console.log('Should disable shadows:', optimizer.shouldDisableShadows())
  
  // 测试自动调整
  const result = optimizer.autoAdjustStrategy(25) // 模拟25fps
  if (result) {
    console.log('Auto-adjusted to:', result)
  }
  
  console.log('✓ Optimization test passed')
}

/**
 * 测试6：动画插值
 */
function testAnimationInterpolation() {
  console.log('=== Test 6: Animation Interpolation ===')
  
  const animManager = new AnimationManager()
  
  // 切换到happy动画
  animManager.switchAnimation('happy', performance.now())
  
  // 获取不同进度的帧
  const timestamps = [0, 200, 400, 600, 800]
  timestamps.forEach(offset => {
    const frame = animManager.getCurrentFrame(performance.now() + offset)
    console.log(`Frame at ${offset}ms:`, frame.body)
  })
  
  console.log('✓ Animation interpolation test passed')
}

/**
 * 运行所有测试
 */
export function runAllTests() {
  console.log('Starting pet renderer tests...\n')
  
  try {
    testBasicRendering()
    testAnimationSwitch()
    testPerformanceMonitoring()
    testRenderLoop()
    testOptimization()
    testAnimationInterpolation()
    
    console.log('\n✅ All tests passed!')
  } catch (error) {
    console.error('\n❌ Test failed:', error)
  }
}

// 导出测试函数
export default {
  testBasicRendering,
  testAnimationSwitch,
  testPerformanceMonitoring,
  testRenderLoop,
  testOptimization,
  testAnimationInterpolation,
  runAllTests
}
